package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.PriceListBo;
import com.zp.base.entity.vo.PriceListVo;
import com.zp.base.service.IPriceListService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 价表控制器
 * 前端访问路由地址为:/base/priceList
 *
 * @author zhang peng
 * @date 2023-01-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/priceList")
public class PriceListController extends BaseController {

    private final IPriceListService iPriceListService;

    /**
     * 查询价表列表 (分页)
     */
    @SaCheckPermission("base:priceList:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PriceListVo> selectPage (PriceListBo bo, PageQuery pageQuery) {
        return iPriceListService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询价表列表 (不分页)
     */
    @SaCheckPermission("base:priceList:list")
    @GetMapping("/list")
    public R<List<PriceListVo>> list (PriceListBo bo) {
        List<PriceListVo> list = iPriceListService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出价表列表
     */
    @SaCheckPermission("base:priceList:export")
    @Log(title = "价表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (PriceListBo bo, HttpServletResponse response) {
        List<PriceListVo> list = iPriceListService.queryList(bo);
        ExcelUtil.exportExcel(list, "价表", PriceListVo.class, response);
    }

    /**
     * 获取价表详细信息
     *
     * @param priceId 主键
     */
    @SaCheckPermission("base:priceList:query")
    @GetMapping("/{priceId}")
    public R<PriceListVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String priceId) {
        return R.ok(iPriceListService.queryById(priceId));
    }

    /**
     * 新增价表
     */
    @SaCheckPermission("base:priceList:add")
    @Log(title = "价表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody PriceListBo bo) {
        return toAjax(iPriceListService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改价表
     */
    @SaCheckPermission("base:priceList:edit")
    @Log(title = "价表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody PriceListBo bo) {
        return toAjax(iPriceListService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除价表
     *
     * @param priceIds 主键串
     */
    @SaCheckPermission("base:priceList:remove")
    @Log(title = "价表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{priceIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] priceIds) {
        return toAjax(iPriceListService.deleteWithValidByIds(Arrays.asList(priceIds), true) ? 1 : 0);
    }

    /**
     * 停价
     *
     * @param priceId
     * @return
     */
    @SaCheckPermission("base:priceList:stopPrice")
    @Log(title = "价表", businessType = BusinessType.STOPPRICE)
    @GetMapping("/stopPrice/{priceId}")
    public R<Void> stopPrice (@PathVariable String priceId) {
        return toAjax(iPriceListService.stopPrice(priceId) ? 1 : 0);
    }

    /**
     * 调价
     *
     * @param bo
     * @return
     */
    @PostMapping("/priceAdjustment")
    @SaCheckPermission("base:priceList:priceAdjustment")
    @Log(title = "价表", businessType = BusinessType.PRICEADJUSTMENT)
    public R<Void> priceAdjustment (@RequestBody PriceListBo bo) {
        return toAjax(iPriceListService.priceAdjustment(bo) ? 1 : 0);
    }

    /**
     * 启用
     *
     * @param bo
     * @return
     */
    @PostMapping("/priceEnable")
    @SaCheckPermission("base:priceList:edit")
    @Log(title = "价表", businessType = BusinessType.PRICEENABLE)
    public R<Void> priceEnable (@RequestBody PriceListBo bo) {
        return toAjax(iPriceListService.priceEnable(bo) ? 1 : 0);
    }

    /**
     * 诊疗项目 自动补全
     *
     * @param bo
     * @return
     */
    @PostMapping("/getCurrentPriceList")
    public R<List<PriceListVo>> getCurrentPriceList (@RequestBody PriceListBo bo) {
        return R.ok(iPriceListService.getCurrentPriceList(bo));
    }

    /**
     * 获取诊疗项目已对照的价表
     *
     * @param bo
     * @return
     */
    @PostMapping("/selectContrastExist")
    public R<List<PriceListVo>> selectContrastExist (@RequestBody PriceListBo bo) {
        return R.ok(iPriceListService.selectContrastExist(bo));
    }
}
